﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FrienDev.BusinessRules;
using System.IO;

public partial class Public_FeedBack : System.Web.UI.Page
{
    private const string m_mailDress = "mmtestmail@sohu.com";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(Request.QueryString["success"]))
        {
            if (UserRules.Instance.IsCurrentUserLogined)
            {
                pn_Contact.Visible = false;
                lb_UserName.Text = UserRules.Instance.GetCurrentUser().FullName;
            }
            bt_Submit.Click += new EventHandler(bt_Submit_Click);
        }
        else
        {
            pn_Write.Visible = false;
            pn_Success.Visible = true;
            if (UserRules.Instance.IsCurrentUserLogined)
            {
                hl_Back.Text = "返回您的个人主页";
                hl_Back.NavigateUrl = ApiHelper.Instance.GetUserHomepageLink(UserRules.Instance.GetCurrentUserId());
            }
            else
            {
                hl_Back.Text = "返回网站首页";
                hl_Back.NavigateUrl = "~/Default.aspx";
            }
        }
    }

    void bt_Submit_Click(object sender, EventArgs e)
    {
        string userName = "";
        string contact = "";
        if (UserRules.Instance.IsCurrentUserLogined)
        {
            userName = UserRules.Instance.GetCurrentUser().FullName;
            contact = UserRules.Instance.GetCurrentUser().Email;
            FeedBackRules.Instance.InsertFeedBack(Convert.ToInt16(ddl_FeedBackType.SelectedValue), txt_FeedBackContent.Text.Trim(), null, UserRules.Instance.GetCurrentUserId());            
        }
        else
        {
            userName = "匿名";
            contact = txt_Contact.Text;
            FeedBackRules.Instance.InsertFeedBack(Convert.ToInt16(ddl_FeedBackType.SelectedValue), txt_FeedBackContent.Text.Trim(), txt_Contact.Text.Trim(), null);
        }

        string mailsubject = "网站意见反馈";
        FileStream fs = new FileStream(MapPathSecure("~/MailTemplate/FeedBack/FeedBackMailTemplate.txt"), FileMode.Open, FileAccess.Read);
        StreamReader m_streamReader = new StreamReader(fs);
        //使用StreamReader类来读取文件
        m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
        string str = m_streamReader.ReadToEnd();
        string mailcontent = string.Format(str, userName, ddl_FeedBackType.SelectedItem.Text, txt_FeedBackContent.Text.Trim(), contact);
        //关闭此StreamReader对象
        m_streamReader.Close();

        try
        {
            ApiHelper.Instance.SendMail(m_mailDress, mailsubject, mailcontent, true);
        }
        catch
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", "javascript:alert('邮件发送失败！');", true);
            return;
        }
        txt_FeedBackContent.Text = "";
        Response.Redirect("/public/FeedBack.aspx?success=1");

    }
}
